<?php
/**
 * XGPHP 轻量级PHP框架
 * @link http://xgphp.xg3.cn
 * @version 2.0.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @author 讯高科技 <xungaokeji@qq.com>
*/
namespace xg;
use \helper\File;
use \helper\Arr;
use \helper\Checker;

class Env {
    protected static $env = [];
    protected static $operate = [];
    protected $data = [];

    /**
     * 读取环境变量定义文件
     * @access public
     * @param string $file 环境变量定义文件
     * @return void
     */
    public function load(string $file)
    {
        $env = self::loadFile($file);
		if(is_array($env)) {
			$this->set($env);
		}
		return $this;
    }

    public static function loadFile(string $file)
    {
		if(is_file($_file=File::path('xg:envs',$file))) {
			$file = $_file;
		}
        $env = parse_ini_file($file, true, INI_SCANNER_RAW);
		if(is_array($env)) {
			foreach($env as $key => $val) {
				if($val == '1' or $val=='true' or $val=='yes') {
					$env[$key] = true;
				}elseif($val == '0' or $val=='false' or $val=='no'){
					$env[$key] = false;
				}
			}
		}
		return $env;
    }
	
	// 获取系统环境
	public static function operate(){
		$env=[];
		$env['os']=php_uname();
		$env['php']=PHP_VERSION;
		$env['software']=$_SERVER['SERVER_SOFTWARE'];
		$env['server']=$env['software'];
		$env['addr']=$_SERVER['SERVER_ADDR'];
		
		// 服务器IP
		if(Checker::isDomain($env['addr'])){	
			// $env['ip']=gethostbyname($env['addr']);
		}elseif(Checker::isIp($env['addr'])){
			$env['ip']=$env['addr'];
		}else{
			$env['ip']='127.0.0.1';
		}
		
		// 判断Apache服务器
		if (stripos($env['software'],'Apache')!==false) {
			$env['apache']=true;
		}
		// 判断Nginx服务器
		if (stripos($env['software'],'Nginx')!==false) {
			$env['nginx']=true;
		}
		// 判断IIS服务器
		if (stripos($env['software'],'Microsoft-IIS')!==false) {
			$env['iis']=true;
		}
		return array_change_key_case($env, CASE_UPPER);
	}
	
	public function set($env, $value = null)
	{
		// PHP环境
		if(!self::$env) {
			self::$env = $_ENV;
		}
		// 运行环境
		if(!self::$operate) {
			self::$operate = self::operate();
		}
		if (is_array($env)) {
			$env = array_change_key_case($env, CASE_UPPER);
			foreach ($env as $key => $val) {
				if (is_array($val)) {
					$val = array_change_key_case($val, CASE_UPPER);
					foreach ($val as $k => $v) {
						$this->data[$key][$k] = $v;
					}
				} else {
					$this->data[$key] = $val;
				}
			}
		} else {
			if(is_string($env)) {
				$env = strtoupper($env);
			}
			$this->data=Arr::set($this->data,$env,$value);
		}
		return $this;
    }
	
	public function data() {
		return $this->data;
    }
	
	public function get(string $name=null,$default=null) {
		$name = strtoupper($name);
		$data = Arr::extend(self::$env,self::$operate,$this->data);
		return Arr::get($data,$name,$default);
	}
    /**
     * 检测是否存在环境变量
     * @access public
     * @param string $name 参数名
     * @return bool
     */
    public function has(string $name)
    {
        return !is_null($this->get($name));
    }

    /**
     * 设置环境变量
     * @access public
     * @param string $name  参数名
     * @param mixed  $value 值
     */
    public function __set(string $name, $value)
    {
        $this->set($name, $value);
    }

    /**
     * 获取环境变量
     * @access public
     * @param string $name 参数名
     * @return mixed
     */
    public function __get(string $name)
    {
        return $this->get($name);
    }	

    /**
     * 检测是否存在环境变量
     * @access public
     * @param string $name 参数名
     * @return bool
     */
    public function __isset(string $name): bool
    {
        return $this->has($name);
    }

    // ArrayAccess
    public function offsetSet(mixed $name, mixed $value): void
    {
        $this->set($name, $value);
    }

    public function offsetExists(mixed $name): bool
    {
        return $this->__isset($name);
    }

    public function offsetUnset(mixed $name): void
    {
        throw new Exception('not support: unset');
    }

    public function offsetGet(mixed $name): mixed
    {
        return $this->get($name);
    }
}
